#include "SrcMgr.h"
//#include "../utils/utils.h"
#include <assert.h>
#include "../lexer/SourceStream.h"


SrcMgr * SrcMgr::m_pInstance = NULL;


SrcMgr::SrcMgr(void)
: m_iNextID(1)
{
	//m_strCurPath = utils::getCurrentPath();
}

SrcMgr::~SrcMgr(void)
{
}

SrcMgr & SrcMgr::getInstance()
{
	if(m_pInstance == NULL)
	{ 
		m_pInstance = new SrcMgr();
	}
	return * m_pInstance;
}

std::pair<SourceID, SourceStream *> SrcMgr::FindSource( const char *strOriginFile, const char * inFile )
{
		
	String includeFile = String(inFile);
	if(strOriginFile == NULL)
	{
		/*if(utils::isFullPath(includeFile))
		{
			m_oSrcs.insert(std::make_pair(m_iNextID,includeFile));
			m_oStreams.insert(std::make_pair(m_iNextID,new SourceStream(m_iNextID)));
			m_iNextID ++;
		}
		else
		{
			String tmp = utils::combineFilePath(m_strCurPath,includeFile);
			m_oSrcs.insert(std::make_pair(m_iNextID,tmp));
			m_oStreams.insert(std::make_pair(m_iNextID,new SourceStream(m_iNextID)));
			m_iNextID ++;
		}*/
		m_oSrcs.insert(std::make_pair(m_iNextID,includeFile));
		std::pair< SrcStreamContainer::iterator,bool> res =m_oStreams.insert(std::make_pair(m_iNextID,new SourceStream(m_iNextID)));
		m_iNextID ++;
		return std::make_pair(res.first->first,res.first->second);
	}
	else
	{
		assert(0);
	}
}

std::pair<SourceID, SourceStream *> SrcMgr::getMainSourceStream() const
{
	SrcStreamContainer::const_iterator it = m_oStreams.find(1);	//Fixit
	if(it == m_oStreams.end())
	{
		assert(0);
	}
	return std::make_pair(1,it->second);
}

String SrcMgr::getFilePath(SourceID fileID) const
{
	SrcContainer::const_iterator it = m_oSrcs.find(fileID);
	if(it == m_oSrcs.end())
		return "";
	return it->second;
}